<?php

App::uses('AppController', 'Controller');

/**
 * PropertyTypes Controller
 *
 */
class PropertyTypesController extends AppController {

    /**
     * Scaffold
     *
     * @var mixed
     */
    public $scaffold;
    public $uses = array('PropertyType', 'Property','Project');

    public function index() {
        $this->set('proTypes', $this->PropertyType->listAll());
    }

    public function add($propertyId) {
        if ($this->request->is('post')) {
            $this->request->data['PropertyType']['property_id'] = $propertyId;
            $this->PropertyType->set($this->request->data['PropertyType']);
            debug($this->request->data);
            if ($this->PropertyType->validates()) {
                $this->PropertyType->save();
                $this->Session->setFlash('Add Success!!');
                $this->redirect('/Properties');
            }
        }
        $this->set('propertyDetail', $this->Property->getDetail($propertyId));
    }
    
    public function delete($propertyTypeId){
        $data = $this->Project->Find('count',array(
            'conditions'=>array(
                'Project.property_type_id'=>$propertyTypeId
            )
        ));
        if($data==0){
            $condition = array('PropertyType.id' => $propertyTypeId);
            $this->PropertyType->deleteAll($condition, false);
            $this->Session->setFlash('Delete Success!!');
                
        }else{
            
        }
        $this->redirect('/Properties');
    }

}
